package com.guiwu.gteway.industryapp.Device.module;

import com.guiwu.gteway.industryapp.Frame.FrameHelper;

/**
 * Created by qiaoguan on 2018/3/24. modbus类型的监控点
 */

public class ModbusMonitor extends Monitor{


    public ModbusMonitor(MonitorData data){
       super(data);
    }

    @Override
    public int getMonitorType() {
        if (modBusRegType.equalsIgnoreCase("01") || modBusRegType.equalsIgnoreCase("02")){
            return BIT;
        }else if (modBusRegType.equalsIgnoreCase("03") || modBusRegType.equalsIgnoreCase("04"))
            return BIT16;
        return 0;
    }

    @Override
    public String getValue() {
        return addDeciaml(value,decimal[1]) + " " + unit;
    }

    @Override
    public String getTypeValue(int inputValue) {
        return  addDeciaml(inputValue,decimal[1]) + " " + unit;
    }

    @Override
    public void sendCmd(String deviceId, String apiKey, boolean coil) {
        String cmdFrame;
        if ((cmdFrame = getCoilFrame(coil)) == null)
            return;
        handleCmd(deviceId,apiKey,cmdFrame);
    }

    @Override
    public void sendCmd(String deviceId, String apiKey, int value) {
        String cmdFrame;
        if ((cmdFrame = getRegFrame(value)) == null)
            return;
        handleCmd(deviceId,apiKey,cmdFrame);
    }

    @Override
    public String getCoilFrame(boolean coil) {
        int address = 0;
        String cmd = null;
        if (coil == getSwitchState())
            return null;
        address = addressAdd;
        if (protocol.equalsIgnoreCase("RTU")) {
            cmd = FrameHelper.writeCoilRTU(slaveAdr, address, coil);
        }else if (protocol.equalsIgnoreCase("ASCII")){
            cmd = FrameHelper.writeCoilASSIC(slaveAdr, address, coil);
        }
        return cmd;
    }

    @Override
    public String getRegFrame(int value) {
        int address = 0;
        String cmd = null;
        //寄存器
        address = addressAdd;
        //协议类型
        if (protocol.equalsIgnoreCase("RTU")){
            cmd = FrameHelper.wrightRegisterRTU(slaveAdr,address,value);
        }else if (protocol.equalsIgnoreCase("ASCII")){
            cmd = FrameHelper.writeRegistASSIC(slaveAdr,address,value);
        }

        return cmd;
    }

    @Override
    public String toString() {
        return "类型：modbus    " + super.toString();
    }
}
